Passed
Branch master (7fff21)
by refat
05:12
created

Background.add   B

Complexity

Conditions 6

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 14
c 0
b 0
f 0
dl 0
loc 21
rs 8.6666
1
// eslint-disable-next-line no-unused-vars
2
class Background {
3
  constructor(options) {
4
    if (options) {
5
      this.elm = Background.formatElm(options.elm);
6
      this.class = Background.formatClass(options.class);
7
    }
8
  }
9
10
  static formatElm(elm) {
11
    if (typeof elm === 'object') return elm;
12
    return document.querySelector(`${elm}`);
13
  }
14
15
  static formatClass(_class) {
16
    if (_class.indexOf('.') === 0) {
17
      return _class.substr(1);
18
    }
19
    return _class;
20
  }
21
22
  addAfter(wait = 0) {
23
    if (wait) {
24
      setTimeout(() => {
25
        this.elm.classList.add(this.class);
26
      }, wait);
27
    } else {
28
      this.elm.classList.add(this.class);
29
    }
30
    return this;
31
  }
32
33
  removeAfter(wait = 0) {
34
    if (wait) {
35
      setTimeout(() => {
36
        this.elm.classList.remove(this.class);
37
      }, wait);
38
    } else {
39
      this.elm.classList.remove(this.class);
40
    }
41
    return this;
42
  }
43
44
  removeAllAfter(wait = 0) {
45
    const elms = document.querySelectorAll(`.${this.class}`);
46
    if (!elms) return;
47
    if (wait) {
48
      setTimeout(() => {
49
        elms.forEach(elm => elm.classList.remove(this.class));
50
      }, wait);
51
    } else {
52
      elms.forEach(elm => elm.classList.remove(this.class));
53
    }
54
  }
55
}
56